package com.qf.smartplatform.config;/*
*王浩政 
*whz
2022/9/5
22:30
*小米商城
*/

import com.alibaba.druid.pool.DruidDataSource;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.TaskExecutor;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.function.Consumer;


     @org.springframework.context.annotation.Configuration//声明当前类是一个配置类，相当于代替了xml文件中的<bean>标签
     @EnableTransactionManagement
     @EnableAsync//开启异步
     public class BeanConfig {
    //<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
    //代替了<bean>标签，返回值的类型就是bean标签内的class，默认情况下，方法的名字就是id
    //注解默认是方法名，也可以自定义注解id
    @Bean
    public DruidDataSource dataSource(){

//        Consumer<Long> consumer = new Consumer<Long>() {
//            @Override
//            public void accept(Long aLong) {
//
//            }
//        }
        DruidDataSource dataSource=new DruidDataSource();
//         <property name="username" value="root"/>
        dataSource.setUsername("root");
//        <property name="password" value="123456"/>
        dataSource.setPassword("123456");
//        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//        <property name="url" value="jdbc:mysql:///smartplatform?useUnicode=true&amp;characterEncoding=utf8"/>
        dataSource.setUrl("jdbc:mysql:///smartplatform?useUnicode=true&characterEncoding=utf8");
        ArrayList<Object> list = new ArrayList<>();
        list.stream();//内部迭代
        return dataSource;
    }
    /*
    * 如果有参数，spring会查找自己管理的对象中有没有这个类型的对象，如果有就注入过来，如果有多个，形参的名字需要和想要引用的对象的名字一致·1
    *
    * */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        //设置分页插件
        sqlSessionFactoryBean.setPlugins(new PageInterceptor());
        //创建配置类
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        //设置驼峰
        configuration.setMapUnderscoreToCamelCase(true);
        //设置日志
        configuration.setLogImpl(StdOutImpl.class);
        //设置配置
        sqlSessionFactoryBean.setConfiguration(configuration);
        return sqlSessionFactoryBean;
    }
/*
* 扫描mapper
* */
@Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
    MapperScannerConfigurer configurer = new MapperScannerConfigurer();
    configurer.setBasePackage("com.qf.smartplatform.mapper");
    return configurer;
}
/*
* 平台事务管理器
* */
@Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource){
    DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
    transactionManager.setDataSource(dataSource);
    return transactionManager;
}

@Bean
    public TaskExecutor taskExecutor(){
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(15);
    executor.setMaxPoolSize(20);
    executor.setThreadNamePrefix("bk2205-");
    //executor.setThreadGroupName("bk2205");
    return executor;
}

 public ObjectMapper objectMapper(){
    return new ObjectMapper();
 }


}
